﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_UserManager_EditorTeacher :PowerCreator.LMS.Web.Base.BasePage
{
    User opUser = new User();

    Job opJob = new Job();
    Duty opDuty = new Duty();
    Department opDepartment = new Department();
    LogicServer.NetMeetingClass objNetMeetingClass;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            //BinderJob();
            //BinderDuty();
            string tid = Request.QueryString["TeacherID"].ToString();
            ViewState["TeacherID"] = tid;
            BinderDepartment();
            BinderPage();
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_AddTeacher.Text = GetLanageValueByKey("lbl_InfoTitle_AddTeacher", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Account_loading.Text = GetLanageValueByKey("lbl_AddTeacher_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_LoadingName.Text = GetLanageValueByKey("lbl_AddTeacher_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_PassWord.Text = GetLanageValueByKey("lbl_AddTeacher_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_AddTeacher_BaseInformaition.Text = GetLanageValueByKey("lbl_AddTeacher_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Name.Text = GetLanageValueByKey("lbl_AddTeacher_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Number.Text = GetLanageValueByKey("lbl_AddTeacher_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Sex.Text = GetLanageValueByKey("lbl_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_IdNumber.Text = GetLanageValueByKey("lbl_AddTeacher_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Born.Text = GetLanageValueByKey("lbl_AddTeacher_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Mail.Text = GetLanageValueByKey("lbl_AddTeacher_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Class.Text = GetLanageValueByKey("lbl_AddTeacher_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Time.Text = GetLanageValueByKey("lbl_AddTeacher_Time", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Job.Text = GetLanageValueByKey("lbl_AddTeacher_Job", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Duty.Text = GetLanageValueByKey("lbl_AddTeacher_Duty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Phone.Text = GetLanageValueByKey("lbl_AddTeacher_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Native.Text = GetLanageValueByKey("lbl_AddTeacher_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Post.Text = GetLanageValueByKey("lbl_AddTeacher_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_MobileTelephone.Text = GetLanageValueByKey("lbl_AddTeacher_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Address.Text = GetLanageValueByKey("lbl_AddTeacher_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Remark.Text = GetLanageValueByKey("lbl_AddTeacher_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_School.Text = GetLanageValueByKey("lbl_AddTeacher_School", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_XueLi.Text = GetLanageValueByKey("lbl_AddTeacher_XueLi", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "(格式：1980-01-01)";
        //lab_TeacherJoinTime.Text = "(格式：1980-01-01)";
        rdbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        RequiredFieldValidator1.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        RequiredFieldValidator3.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        btn_AddNew.Text = GetLanageValueByKey("btn_EditNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderPage()
    {
        VOTTeacher vo = opUser.LoadTeacher(int.Parse(ViewState["TeacherID"].ToString()));
        if (vo != null)
        { 
            VOTUser voUser = opUser.LoadUser(vo.FKTeacherUserId);

            if (voUser != null)
            {
                txt_UserName.Text = voUser.UserName;
                txt_UserloginIP.Text = voUser.UserLoginIP;
            }
            txt_RealName.Text = vo.TeacherName;
            txt_TeacherAddress.Text = vo.TeacherAddress;
            if (vo.TeacherBirthday != null)
                txt_TeacherBirthday.Text = vo.TeacherBirthday.ToShortDateString();
            txt_TeacherCode.Text = vo.TeacherCode;
            txt_TeacherDescription.Text = vo.TeacherDescription;
            txt_TeacherEmail.Text = vo.TeacherEmail;
            txt_TeacherGraduateSchool.Text = vo.TeacherGraduateSchool;
            txt_TeacherHomeplace.Text = vo.TeacherHomeplace;
            txt_TeacherIdentityCard.Text = vo.TeacherIdentityCard;
            if(vo.TeacherJoinTime != null)
                txt_TeacherJoinTime.Text = vo.TeacherJoinTime.ToShortDateString();
            txt_TeacherMobileTelephone.Text = vo.TeacherMobileTelephone;
            txt_TeacherPost.Text = vo.TeacherPost;
            txt_TeacherStudLevel.Text = vo.TeacherStudLevel;
            txt_TeacherTelephone.Text = vo.TeacherTelephone;
            ddl_Department.SelectedValue = vo.FKTeacherDepartmentId.ToString();
            //ddl_Duty.SelectedValue = vo.FKTeacherTeadutyId.ToString();
            //ddl_Job.SelectedValue = vo.FKTeacherTeajobId.ToString();
            
            if (vo.TeacherSex == 0)
            {
                rdbtn_Male.Checked = true;
                rdbtn_Female.Checked = false;
            }
            else
            {
                rdbtn_Male.Checked = false;
                rdbtn_Female.Checked = true;
            }

            if (!string.IsNullOrEmpty(vo.FKTeacherDepartmentId.ToString()))
            {
                for (int i = 0; i < ddl_Department.Items.Count; i++)
                {
                    if (ddl_Department.Items[i].Value == vo.FKTeacherDepartmentId.ToString())
                    {
                        ddl_Department.Items[i].Selected = true;
                        break;
                    }
                }
            }
            //if (!string.IsNullOrEmpty(vo.FKTeacherTeajobId.ToString()))
            //{
            //    for (int i = 0; i < ddl_Job.Items.Count; i++)
            //    {
            //        if (ddl_Job.Items[i].Value == vo.FKTeacherTeajobId.ToString())
            //        {
            //            ddl_Job.Items[i].Selected = true;
            //            break;
            //        }
            //    }
            //}

            //if (!string.IsNullOrEmpty(vo.FKTeacherTeadutyId.ToString()))
            //{
            //    for (int i = 0; i < ddl_Duty.Items.Count; i++)
            //    {
            //        if (ddl_Duty.Items[i].Value == vo.FKTeacherTeadutyId.ToString())
            //        {
            //            ddl_Duty.Items[i].Selected = true;
            //            break;
            //        }
            //    }
            //}
        }
    }
    //绑定部门类型信息
    private void BinderDepartment()
    {
        string search = "DepartmentType=1";
        DataTable dt = opDepartment.Query(search);
        ArrayList list = new ArrayList();
        list = opDepartment.GetDepartmentTree(dt);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
    }
    //private void BinderJob()
    //{
    //    string search = "";
    //    DataTable dt = opJob.Query(search);
    //    ddl_Job.DataSource = dt;
    //    ddl_Job.DataTextField = "TeajobName";
    //    ddl_Job.DataValueField = "TeajobID";
    //    ddl_Job.DataBind();
    //}
    //private void BinderDuty()
    //{
    //    string search = "";
    //    DataTable dt = opDuty.Query(search);
    //    ddl_Duty.DataSource = dt;
    //    ddl_Duty.DataTextField = "TeadutyName";
    //    ddl_Duty.DataValueField = "TeadutyID";
    //    ddl_Duty.DataBind();
    //}
    private bool CheckPageData()
    {
        if (!string.IsNullOrEmpty(txt_TeacherBirthday.Text))
        {
            if (!TimeUtils.IsDateTime(txt_TeacherBirthday.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_TeacherBirthday.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_TeacherBirthday.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_TeacherJoinTime.Text))
        {
            if (!TimeUtils.IsDateTime(txt_TeacherJoinTime.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_TeacherJoinTime.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_TeacherJoinTime.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_TeacherEmail.Text))
        {
            if (!PageValidate.IsEmail(txt_TeacherEmail.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_TeacherEmail.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_TeacherEmail.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_TeacherTelephone.Text))
        {
            if (!PageValidate.IsPhone(txt_TeacherTelephone.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_TeacherTelephone.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_TeacherTelephone.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_TeacherMobileTelephone.Text))
        {
            if (!PageValidate.IsNumber(txt_TeacherMobileTelephone.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_TeacherMobileTelephone.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_TeacherMobileTelephone.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }

        //if (!string.IsNullOrEmpty(txt_TeacherTelephone.Text))
        //{
        //    if (!PageValidate.IsNumber(txt_TeacherTelephone.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g6", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_TeacherTelephone.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_TeacherTelephone.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTTeacher vo = opUser.LoadTeacher(int.Parse(ViewState["TeacherID"].ToString()));

        VOTUser voUser = opUser.LoadUser(vo.FKTeacherUserId);
        voUser.UserLoginIP = txt_UserloginIP.Text;
        opUser.Update(voUser);

        vo.TeacherTelephone = txt_TeacherTelephone.Text;
        vo.TeacherStudLevel = txt_TeacherStudLevel.Text;
        if (rdbtn_Male.Checked)
            vo.TeacherSex = 0;
        else
            vo.TeacherSex = 1;
        vo.TeacherPost = txt_TeacherPost.Text;
        vo.TeacherName = txt_RealName.Text;
        vo.TeacherMobileTelephone = txt_TeacherMobileTelephone.Text;
        if(!string.IsNullOrEmpty(txt_TeacherJoinTime.Text))
            vo.TeacherJoinTime = DateTime.Parse(txt_TeacherJoinTime.Text);
        vo.TeacherIdentityCard = txt_TeacherIdentityCard.Text;
        vo.TeacherHomeplace = txt_TeacherHomeplace.Text;
        vo.TeacherGraduateSchool = txt_TeacherGraduateSchool.Text;
        vo.TeacherEmail = txt_TeacherEmail.Text;
        vo.TeacherDescription = txt_TeacherDescription.Text;
        vo.TeacherCode = txt_TeacherCode.Text;
        if(!string.IsNullOrEmpty(txt_TeacherBirthday.Text))
            vo.TeacherBirthday = DateTime.Parse(txt_TeacherBirthday.Text);
        vo.TeacherAddress = txt_TeacherAddress.Text;
        if (!string.IsNullOrEmpty(ddl_Department.SelectedValue.ToString()))
            vo.FKTeacherDepartmentId = int.Parse(ddl_Department.SelectedValue.ToString());
       
        //if (!string.IsNullOrEmpty(ddl_Duty.SelectedValue.ToString()))
        //    vo.FKTeacherTeadutyId = int.Parse(ddl_Duty.SelectedValue.ToString());
        vo.FKTeacherTeadutyId = 0;
        //if (!string.IsNullOrEmpty(ddl_Job.SelectedValue.ToString()))
        //    vo.FKTeacherTeajobId = int.Parse(ddl_Job.SelectedValue.ToString());
        vo.FKTeacherTeajobId = 0;

        if (opUser.UpdateTeacher(vo))
        {
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass.WebModifyUser(voUser.UserId, voUser.UserName, voUser.UserPassword, (ulong)voUser.UserRule, voUser.UserImageIndex, vo.TeacherDescription, vo.TeacherSex, vo.TeacherMobileTelephone, voUser.UserType);
            }
           
            string url = "TeacherManager.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "TeacherManager.aspx";
        Script.RedirectURL(url);
    }
}
